// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using SoapTrace.Core.Communication;
using System;
using System.Text;

namespace SoapTrace.Tests.Utils
{
	/// <summary>
	/// Represents a complete message sent to or received from the
	/// remote server.
	/// </summary>
	public class MockRemoteServerMessage
	{
		/// <summary>
		/// A dynamic byte array holding the message bytes.
		/// </summary>
		private ByteArray m_Bytes = new ByteArray( );

		/// <summary>
		/// Creates a new instance of this class.
		/// </summary>
		public MockRemoteServerMessage( )
		{
		}

		/// <summary>
		/// Converts a message into a byte array that can be sent to a server.
		/// </summary>
		/// <param name="message">The message.</param>
		/// <returns>The message converted to a byte array.</returns>
		public static byte[ ] GetBytes( string message )
		{
			string MessageToSend = String.Concat( message, "\r\n" );

			return UnicodeEncoding.UTF8.GetBytes( MessageToSend );
		}

		/// <summary>
		/// Clears the message.
		/// </summary>
		public void Clear( )
		{
			m_Bytes.Clear( );
		}

		/// <summary>
		/// Gets the message as a string.
		/// </summary>
		/// <returns>The message string.</returns>
		public string GetString( )
		{
			byte[ ] Bytes = m_Bytes.GetBytes( );

			return UnicodeEncoding.UTF8.GetString( Bytes );
		}

		/// <summary>
		/// Adds bytes to the message.
		/// </summary>
		/// <param name="bytes">The bytes to add.</param>
		/// <param name="length">The length of the buffer that contains
		/// bytes.</param>
		/// <returns>A flag indicating whether a complete message 
		/// was received.</returns>
		public bool AddBytes( byte[ ] bytes, int length )
		{
			bool AddBytes = false;

			for( int i = 0; ( i < length ) && ( AddBytes == false ); ++i )
			{
				AddBytes = AddByte( bytes[ i ] );
			}

			return AddBytes;
		}

		/// <summary>
		/// Adds a single byte to the message.
		/// </summary>
		/// <param name="messageByte">The message byte to add.</param>
		/// <returns>A status flag indicating whether the byte added
		/// was the end of the message.</returns>
		public bool AddByte( byte messageByte )
		{
			bool AddByte = false;

			if( messageByte == '\n' )
			{
				AddByte = true;
			}
			else if( messageByte == '\r' ) 
			{
				// Ignore end of message marker.
			}
			else
			{
				m_Bytes.Add( messageByte );
			}

			return AddByte;
		}

	}
}
